package com.carryGo.web.dp;

import com.carryGo.entity.Commodity;
import com.carryGo.entity.PageManager;
import com.carryGo.service.dp.CommodityService;
import com.carryGo.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

/**
 * ClassName:CommodityServlet
 * package:guoban
 * Desription:
 *  实现商品的功能
 * @Date:2020/8/27 12:41
 * @Author: 邓攀
 */

@WebServlet("/commodityDp")
@MultipartConfig
public class CommodityServlet extends HttpServlet {

    private CommodityService commodityService = new CommodityService();
    private HttpServletRequest request;
    private HttpServletResponse response;

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        synchronized (this) {
            this.request = request;
            this.response = response;
            String method  = request.getParameter("method");

            switch (method) {
                case "showPic": showPic(); break;
                case "toCanyinyule": toCanyinyule(); break;
                case "toErshoushichang": toErshoushichang(); break;
            }
        }

    }

    private void toErshoushichang() throws ServletException, IOException {
        PageManager pm = WebUtils.getBean(PageManager.class,request);
        List<Commodity> commodities = commodityService.queryCommodityVehicle(pm);
        int totalCount = commodityService.queryVehicleTotalCount();
        pm.setTotalCount(totalCount);
        pm.setPages();
        request.getSession().setAttribute("pm",pm);
        request.setAttribute("commodities",commodities);
        WebUtils.toRequestPage(request,response,"ershoushichang");
    }

    private void toCanyinyule() throws ServletException, IOException {
        PageManager pm = WebUtils.getBean(PageManager.class,request);
        List<Commodity> commodities = commodityService.queryCommodities(pm);
        int totalCount = commodityService.queryUserTotalCount();
        pm.setTotalCount(totalCount);
        pm.setPages();
        request.getSession().setAttribute("pm",pm);
        request.setAttribute("commodities",commodities);
        WebUtils.toRequestPage(request,response,"canyinyule");
    }

    private void showPic() throws IOException {
        String pic = request.getParameter("pic");
        if (pic == null) return;
        File file = new File(pic);
        if (!file.exists()) return;
        //将图片的字节写入到页面
        InputStream in = new BufferedInputStream(new FileInputStream(file));
        OutputStream out = response.getOutputStream();
        byte[] b = new byte[1024];
        int len = 0;
        while ((len = in.read(b)) != -1) {
            out.write(b, 0, len);
        }
        out.flush();
        out.close();
        in.close();
        response.flushBuffer();//刷新缓存
    }
}
